package com.tecode.controller;

import com.tecode.service.MusicService;
import com.tecode.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class StatisticsController {
    @Autowired
    private MusicService musicService;
    @Autowired
    private UserService userService;

    /**
     * 跳转charts-1
     * @return
     */
    @RequestMapping("/charts-1.html")
    private String toStatisticsMusic(){
        return "charts-1";
    }

    /**
     * @author Liuqi
     * 统计各类音乐数量
     */
    @RequestMapping("/statisticsMusic")
    @ResponseBody
    public List<Object> statisticsByCodeMusicNum(){
        return musicService.selectMusicByCodeNumAndStyle();
    }

    /**
     * 跳转charts-2
     * @return
     */
    @RequestMapping("/charts-2.html")
    private String toStatisticsUser(){
        return "charts-2";
    }
    /**
     * @author liuqi
     * 统计音乐人和普通用户
     */
    @RequestMapping("/statisticsUser")
    @ResponseBody
   public List<Map> selectUserByCodeNum(){
       return userService.selectUserByCodeNum();
   }


    /**
     * 跳转charts-3.html
     * @return
     */
   @RequestMapping("/charts-3.html")
    public String toAccessStatistics(){
       return "charts-3.html";
   }
   @RequestMapping("/accessStatistics")
   @ResponseBody
   public List<Map> selectAccessStatistics() throws ParseException {
        List times = new ArrayList();
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       String specifiedDay = df.format(new Date());
       Calendar c = Calendar.getInstance();
       Date date=null;
       date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
       c.setTime(date);
       int day=c.get(Calendar.DATE);
       times.add(specifiedDay);
       for (int i=1;i<7; i++){
           c.set(Calendar.DATE,day-i);
           String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
           times.add(dayBefore);

       }
       Date min = df.parse((String) times.get(6));
       Date max = df.parse((String) times.get(0));
       java.sql.Date minTime = new java.sql.Date(min.getTime());
       java.sql.Date maxTime = new java.sql.Date(max.getTime());
       Map minAndMax = new HashMap();
       minAndMax.put("minTime",minTime);
       minAndMax.put("maxTime",maxTime);
       List<Map> listStatistics = userService.selectAccessStatistics(minAndMax);

       for (int j=0;j<times.size(); j++){
           int count =0;
           for (int i=0; i<listStatistics.size(); i++){
               if(listStatistics.get(i).get("access_time").toString().equals(times.get(j))){
                   count++;
               }
           }
           if (count==0){
               Map statistics = new HashMap();
               statistics.put("num",0);
               statistics.put("access_time",times.get(j));
               listStatistics.add(statistics);

               for (int n = 0; n < listStatistics.size(); n++) {
                   for (int  m= listStatistics.size() - 1; m > n; m--) {

                       Date dates = df.parse(listStatistics.get(m).get("access_time").toString());
                       Date t = df.parse(listStatistics.get(m - 1).get("access_time").toString());

                       if (dates.compareTo(t) < 0) {
                           //互换位置
                           Map<String, String> stu = listStatistics.get(m);
                           listStatistics.set(m, listStatistics.get(m - 1));
                           listStatistics.set(m - 1, stu);
                       }
                   }
               }
           }
       }
        return listStatistics;
   }
}
